package net.huashitong.zhApp.service;

import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.oversee.entity.Coordinate;
import net.huashitong.oversee.filter.CoordinateFilter;
import net.huashitong.oversee.service.CoordinateService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.utils.UtilsTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppQuestionReportService {

    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private NodeNewschedulesService nodeService;
    @Autowired
    private CoordinateService coordinateService;

    /**
     * 创建提请协调
     *
     * @param filter
     * @param user
     */
    public boolean createQuestion(CoordinateFilter filter, BnpUser user) {
        boolean flag = false;
        //根据projectId,查询项目
        String projectId = filter.getProjectId();
        NewProjects project = newProjectsService.getById(projectId);
        NodeNewschedules node = nodeService.getNodeNewschedulesByProjectIdAndNodeId(project.getSysId(), filter.getNodeId());

        Coordinate question = new Coordinate();
        //状态 "1" "正在处理"; "3" "处理完毕"; "2" "转为攻坚督查"; "0" "撤销协调";
        question.setAaa001("1");
        question.setBaseInfo(project.getName() + " " + node.getNodeId() + " " + node.getNodeName() + " " + "提请协调");
        question.setContact(project.getContactName());
        question.setfGcountyLeader(project.getResUser());
        question.setfGcountyLeaderPhone(project.getResPhone());
        question.setNodeSysId(node.getSysId());
        question.setNodeId(node.getNodeId());
        question.setNodeName(node.getNodeName());
        String involveUnit = filter.getInvolveUnit();       //选择的涉及部门
        if (StringUtils.isBlank(involveUnit)) {
            involveUnit = node.getGroupName();              //节点的督查组
            if (StringUtils.isBlank(involveUnit)) {
                involveUnit = project.getGroupName();       //项目的督查组
            }
        }
        question.setInvolveUnit(involveUnit);
        question.setHelpInfo(filter.getHelpInfo());
        question.setProjectName(project.getName());
        question.setResDept(project.getResDept());
        question.setUserId(user.getSysId());
        question.setReportTime(UtilsTools.getCurrentDateTime());
        question.setReportUser(user.getName());
        question.setReportUserPhone(user.getPhone());
        question.setOperator(node.getOperatorUser());
        question.setOperatorPhone(node.getOperatorPhone());
        question.setResUser(node.getResUser());
        question.setResUserPhone(node.getResUserPhone());
        question.setOrganizer(project.getResDept());
        coordinateService.save(question);
        //发送短信
        coordinateService.sendMessage(question);
        flag = true;
        return flag;
    }


    /**
     * 修改提请协调（APP端）
     *
     * @param filter
     */
    public boolean updateQuestion(CoordinateFilter filter) {
        boolean flag = false;
        //根据projectId,查询项目
        Coordinate question = coordinateService.getById(filter.getSysId());
        String helpInfo = filter.getHelpInfo();
        String involveUnit = filter.getInvolveUnit();
        if (StringUtils.isNotBlank(helpInfo)) {
            question.setHelpInfo(helpInfo);
        }
        if (StringUtils.isNotBlank(involveUnit)) {
            question.setInvolveUnit(involveUnit);
        }
        coordinateService.update(question);
        flag = true;
        return flag;
    }
}
